﻿using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.WarehouseManagementCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.WarehouseManagementCommandHandler
{
    public class DelWareHouseCommandHandler : IRequestHandler<DelWareHouseCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;

        public DelWareHouseCommandHandler(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<ApiResult<object>> Handle(DelWareHouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnum.删除成功;
            result.MAG = "删除成功";

            var itemsToDelete = await db.Queryable<WareHouse>().Where(p => p.WareHouseID == request.WareHouseID).ToListAsync();
            await db.Deleteable(itemsToDelete).ExecuteCommandAsync();

            return result;
        }
    }
}
